/**
 * 创建窗口，避免连续点击不同菜单内容，使module导入与相应窗口出现混乱
 * 
 **/
package cn.com.liuwenquan.flex.views
{
	import cn.com.liuwenquan.flex.model.SessionModelLocator;
	
	import flash.display.DisplayObject;
	
	import flexlib.containers.SuperTabNavigator;
	
	import mx.containers.VBox;
	import mx.controls.Label;
	import mx.events.ModuleEvent;
	import mx.modules.IModuleInfo;
	import mx.modules.ModuleManager;
	
	public class CreateTableContent
	{
		private var vb:VBox = new VBox();  //save桌面生成的窗口,以便加载module成功后，将其放入vbox中
		private var html:Boolean = false;
		[Embed(source="./fleximages/document.png")]
		private var document_icon:Class;
		[Bindable]
    	public var session:SessionModelLocator = SessionModelLocator.getInstance();
    	[Bindable]
    	public var addIFrameTabModule:Array = new Array();  //保存使用html iframe的module
		
		static private var _instance:CreateTableContent = null;
		
		static public function getInstance():CreateTableContent
		{
			if(_instance == null)
			{
				_instance = new CreateTableContent();
			}
			return _instance;
		}
		
		/**
	     * noComponent:String 调用组件编号
		 * titleName:String   窗口名称
	     **/
		public function addTab(noComponent:String, titleName:String, navigator:SuperTabNavigator, 
		                       contentString:String=null, icon:Class=null):void {
			if(titleName=="") titleName = "(Untitled)";
				
			var curNum:Number = navigator.numChildren + 1;
			//使用VBox生成窗口，其他组件要放在这个vbox中
			var child:VBox = new VBox();
				
			child.setStyle("closable", true);
			child.label = titleName;
			child.data = noComponent;
				
			//将显示的supertab保存
			if(session.tabObjectArray == null){
				session.tabObjectArray = new Array(child);
			}else{
				session.tabObjectArray.push(child);
			}
				
			if(icon) {
				child.icon = icon;
			}
			else {
				child.icon = document_icon;
			}
            var url:String = null;
            switch(noComponent)
            {
            	case "001": url = "desk_main_module.swf"; break;
//            	case "01001": url = "cn/module/component/baseInfo/view/CompanyInfo_Module.swf"; break;  //公司介绍
//            	case "01002": url = "cn/module/component/baseInfo/view/CompanyNews_Module.swf"; break;  //公司新闻
//            	case "01003": url = "cn/module/component/baseInfo/view/JiaMeng_Module.swf"; break;      //加盟政策
//            	case "01004": url = "cn/module/component/baseInfo/view/ZhaoPin_Module.swf"; break;      //招聘岗位
//            	case "01005": url = "cn/module/component/baseInfo/view/Knowledge_Module.swf"; break;      //行业知识
//            	case "01006": url = "cn/module/component/baseInfo/view/ContactUs_Module.swf"; break;      //联系我们
//            	case "01007": url = "cn/module/component/baseInfo/view/Notes_Module.swf"; break;      //留言
            	
            	case "02010": url = "cn/module/component/productManager/view/ProductsType_Module.swf"; break;      //产品类别
            	case "02011": url = "cn/module/component/productManager/view/Products_Module.swf"; break;      //产品管理
            	case "02012": url = "cn/module/component/productManager/view/Products_Out_Module.swf"; break;      //产品管理
//            	case "03022": url = "cn/module/component/baseInfo/view/PersonInfo_Module.swf"; break;   //个人信息
//            	case "04032": url = "cn/module/component/baseInfo/Calender_WNL_Module.swf"; break;  //万年历
		    	default: var label:Label = new Label(); 
		    	label.text = contentString == null ? "对不起，此项目正在紧张制作中...尽请关注！" + titleName : contentString;
		    	child.addChild(label);
		    	url = "cn/module/component/customerinfo/view/Customer_Factory_Info_Module.swf"; break;
            }
            vb = new VBox();
            vb = child;   //后面的函数使用，给对象赋值。先准备vb，再导入swf模块
            loadModule(url);
			//窗口加入组件
			navigator.addChild(child);
		}
		//全局变量
		private var m:IModuleInfo;
		    
		private function loadModule(url:String):void{
		    m = ModuleManager.getModule(url); //设置Module地址，地址是编译后swf在bin中的位置
		    //设置事件监听
		    m.addEventListener(ModuleEvent.READY,loadReady);
		    m.addEventListener(ModuleEvent.PROGRESS,loading);
		    m.addEventListener(ModuleEvent.ERROR,loadError);
		    m.load(); //加载Module
		}
		//Module加载完成
        private function loadReady(e:ModuleEvent):void{
            //将Module对象转换为PictureWindowInterface
            var ml:DesktopTableInterface = e.module.factory.create() as DesktopTableInterface;
            this.vb.addChild(ml as DisplayObject);
            //ml.setSelectIndex(1); //通过Interface调用Module中的方法
            if(html){
            	addIFrameTabModule.push(ml);
            }
            html = false;
        }
        //Module加载中......
		private function loading(e:ModuleEvent):void{
		    trace("显示Loading提示----"+this.vb.label)
		}
		//Module加载失败
		private function loadError(e:ModuleEvent):void{
		    trace("错误处理")
		}
	}
}